﻿using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class PlayControl : MonoBehaviour
{

    Transform m;
    float CurrentSpeed;
    public float MoveSpeed;
    public float Force;
    public float SpeedMax;


    //Rigidbody rig;
    void Start()
    {
        m = this.transform;
        StartCoroutine("Move");
        this.CurrentSpeed = this.MoveSpeed;
        StartCoroutine("AddSpeed");
        StartCoroutine("Jump");
        StartCoroutine("ResetSpeed");
                   
    }

    void Update()
    {
        if(m.position.y<0.49f)
        {
            this.enabled = false;
            SceneManager.LoadScene("tongji");

        }

    }

    //玩家通过WSAD移动
    IEnumerator Move()
    {
        while (true)
        {          
            this.m.Translate(new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")) * CurrentSpeed * Time.deltaTime);
            yield return 0;
        }
     
    }

    //按住Shift进行移动加速
    IEnumerator AddSpeed()
    {
        while (true)
        {
            if (Input.GetButton("Fire3"))
            {
                this.CurrentSpeed = 2 * MoveSpeed;
            }

            if (Input.GetButtonUp("Fire3"))
            {
                this.CurrentSpeed = MoveSpeed;
            }
            yield return 0;
        }
    }

    //Space键实现玩家跳跃,向前上方跳
    IEnumerator Jump()
    {
        while(true)
        {
            if(Input.GetButtonDown("Jump"))
            {
                Rigidbody rig = this.gameObject.GetComponent<Rigidbody>();
                rig.AddForce(new Vector3(0,1,1)*Force);
            }
            yield return 0;
        }
    }

    int num = 0;//记录金币的个数
    int num2 = 0;//记录加速道具的个数
    int num3= 0;//记录护身罩的个数
    void OnTriggerEnter(Collider collider)
    {
        if (collider.gameObject.layer == LayerMask.NameToLayer("money"))//捡金币
        {          
            //Destroy(collider.gameObject);
            collider.gameObject.GetComponent<MeshRenderer>().enabled = false;//设置为false,使物体看不见
            num++;
        }

        if (collider.gameObject.layer == LayerMask.NameToLayer("addSpeed"))//加速
        {
            this.CurrentSpeed += MoveSpeed;//每吃个加速道具,速度都增加
            if (this.CurrentSpeed > SpeedMax)//如果速度大于最大速度，那么当前速度为SpeedMax
            {
                this.CurrentSpeed = SpeedMax;
            }
            //Destroy(collider.gameObject);
            collider.gameObject.GetComponent<MeshRenderer>().enabled = false;//设置为false,使物体看不见
            num2++;
        }

        if (collider.gameObject.layer == LayerMask.NameToLayer("protect"))//护身罩
        {
            //Destroy(collider.gameObject);
            collider.gameObject.GetComponent<MeshRenderer>().enabled = false;//设置为false,使物体看不见
            num3++;
        }
    }

    float t = 0f;
    IEnumerator ResetSpeed()//当速度达到最大时,3s后速度为最开始的速度
    {
        while(true)
        {
            if (this.CurrentSpeed == SpeedMax) t += Time.deltaTime;
            else t = 0;

            if(t>=3)
            {
                this.CurrentSpeed = MoveSpeed;
            }
            yield return new WaitForFixedUpdate();
        }
    }

    //int num4= 0;//记录碰撞前拥有的护身罩,用来清0作用
    void OnCollisionEnter(Collision collision)//碰撞到障碍物时,停止,跳转到统计页面
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("bar") && num3 >= 1)
        {           
            collision.gameObject.GetComponent<BoxCollider>().isTrigger = true;
            num3 = 0;
        }

        else if (collision.gameObject.layer == LayerMask.NameToLayer("bar") && num3 ==0)
        {
            this.enabled = false;
            SceneManager.LoadScene("tongji");
        }

    }

    int score=0;
    void OnGUI()
    {          
        GUI.Label(new Rect(10, 0, 150, 50), "游戏总运行时间：" + (int)Time.time);
        GUI.Label(new Rect(10, 30, 150, 50), "当前关卡总运行时间：" + (int)Time.timeSinceLevelLoad);
        
        float distance = Vector3.Distance(new Vector3(0,0.5f,20),m.position);
        if (distance == 0.0f)//如果Cube在起始位置，那么金币数、加速道具数和分数都为0，
        {
            num = 0;
            score = 0;
            num2 = 0;
        }
        else
        {
            score = (int)distance + num * 5 + num2 * 2;//所得分数的计算，1单位的距离1分，捡一个金币5分          
        }
        GUI.Label(new Rect(10, 50, 150, 50), "吃的金币数是" + num);
        PlayerPrefs.SetInt("num", num); //数据放入缓存中
        //GUI.Label(new Rect(10, 70, 150, 50), "加速道具数：" + num2);//关卡不需要体现
        PlayerPrefs.SetInt("num2", num2);
        PlayerPrefs.SetInt("num3", num3); 
        GUI.Label(new Rect(10, 90, 150, 50), "所得分数：" + score);
        PlayerPrefs.SetInt("score", score);        
        //PlayerPrefs.Save();//保存数据
    }


 
}
